import time
import requests
import hashlib

import json


def ku(t):
    if isinstance(t, list):
        # 递归处理数组中的每个元素
        for r, o in enumerate(t):
            if isinstance(o, dict):
                # 遍历字典的每个键值对
                for i in list(o.keys()):
                    if o[i] is None:
                        # 删除字典中的 None 值
                        del o[i]
                    elif isinstance(o[i], list):
                        # 递归处理嵌套数组
                        ku(o[i])
                        # 将处理后的数组转换为 JSON 字符串，并去除两端的空白字符
        return json.dumps(t).strip()
    elif isinstance(t, dict):
        # 如果 t 是字典，直接转换为 JSON 字符串
        return json.dumps(t)
    else:
        # 如果 t 不是数组或字典，直接返回 t
        return t


def Ou(e):
    # 提取字典e的键，并转换为列表
    t = list(e.keys())
    # 对键列表进行排序
    t_sorted = sorted(t)
    # 返回排序后的键列表
    return t_sorted


def Cu(e):
    t = Ou(e)
    n = ""
    for i in t:
        r = ku(e[i])
        if r is not None and str(r) != "":
            n += f"{i}={r}&"
    # 移除字符串末尾可能存在的'&'字符
    if n and n.endswith('&'):
        n = n[:-1]
    return n


def calculate_md5(data):
    # 创建一个 md5 hash 对象
    md5_hash = hashlib.md5()

    # 更新哈希对象的数据
    md5_hash.update(data.encode('utf-8'))

    # 获取十六进制格式的哈希值
    hex_dig = md5_hash.hexdigest()

    return hex_dig


def Su(e, t, time):
    n = str(t) + str(e) + str(time)
    res = calculate_md5(n)
    return res


cookies = {
    'Hm_lvt_03b8714a30a2e110b8a13db120eb6774': '1712991462,1712991545',
    'Hm_lpvt_03b8714a30a2e110b8a13db120eb6774': '1712991545',
    'HWWAFSESTIME': '1712991542113',
    'HWWAFSESID': 'ae729a3eda293f35b8',
}

headers = {
    'Content-Type': 'application/json;charset=UTF-8',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
    'sign': '',
    'timestamp': '',
}

json_data = {
    'eid': '',
    'achievementQueryType': 'and',
    'achievementQueryDto': [],
    'personnelQueryDto': {
        'queryType': 'and',
    },
    'aptitudeQueryDto': {
        'queryType': 'and',
        'nameStr': '',
        'aptitudeQueryType': 'and',
        'businessScopeQueryType': 'or',
        'filePlaceType': '1',
        'aptitudeDtoList': [
            {
                'codeStr': '',
                'queryType': 'and',
                'aptitudeType': 'qualification',
            },
        ],
        'aptitudeSource': 'new',
    },
    'page': {
        'page': 1,
        'limit': 20,
        'field': '',
        'order': '',
    },
}
"1713025967795"
'1713026509559'
timestamp_ = str(int(time.time() * 1000))
# timestamp_ = "1713027839482"
t = Cu(json_data).replace(" ", "") + "&"
sign = Su("ghaepVf6IhcHmgnk4NCTXLApxQkBcvh1",
          Su("mwMlWOdyM7OXbjzQPulT1ndRZIAjShDB", Su("ZuSj0gwgsKXP4fTEz55oAG2q2p1SVGKK", t, timestamp_), timestamp_),
          timestamp_)
headers["sign"] = sign
headers["timestamp"] = timestamp_
"https://capi.jiansheku.com/nationzj/enterprice/page"
# response = requests.post('https://capi.jiansheku.com/nationzj/enterprice/page', cookies=cookies, headers=headers, data=json_data)
response = requests.post('https://capi.jiansheku.com/nationzj/enterprice/page', cookies=cookies, headers=headers, json=json_data)

print(response.json())
